<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" title="字典列表" width="800px">
    <BasicTable @register="registerTable" :rowClassName="getRowClassName">
      <template #tableTitle>
        <a-button type="primary" @click="handleCreate"> 新增</a-button>
      </template>
      <template v-slot:bodyCell="{ column, record, index }">
        <template v-if="column.dataIndex === 'action'">
          <TableAction :actions="getTableAction(record)" />
        </template>
      </template>
    </BasicTable>
  </BasicDrawer>
  <DictItemModal @register="registerModal" @success="reload" :dictId="dictId" />

  <!-- 复制确认弹窗 -->
  <BasicModal @register="registerCopyModal" title="复制字典项" @ok="handleCopyConfirm" :confirm-loading="copyLoading">
    <template #default>
      <a-input v-model:value="copyInputValue" placeholder="请输入新的字典项名称" style="margin-top: 16px" />
    </template>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/src/components/Drawer';
  import { BasicTable, useTable, TableAction } from '/src/components/Table';
  import { useModal, BasicModal } from '/src/components/Modal';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { Input as AInput } from 'ant-design-vue';
  import DictItemModal from './DictItemModal.vue';
  import { dictItemColumns, dictItemSearchFormSchema } from '../dict.data';
  // import { itemList, deleteItem} from '../dict.api'; // 假设新增了copyItem接口
  import { itemList, deleteItem, copyItem } from '../dict.api'; // 假设新增了copyItem接口
  import { ColEx } from '/@/components/Form/src/types';

  const { prefixCls } = useDesign('row-invalid');
  const dictId = ref('');
  // 复制相关变量
  const copyInputValue = ref('');
  const copyLoading = ref(false);
  const currentCopyRecord = ref(null);

  // 字典配置model
  const [registerModal, { openModal }] = useModal();

  // 复制弹窗配置
  const [registerCopyModal, { openModal: openCopyModal, closeModal }] = useModal({
    maskClosable: false,
    destroyOnClose: true,
    onOpen: () => {
      // 打开时清空输入框
      copyInputValue.value = '';
    },
  });

  const [registerDrawer] = useDrawerInner(async (data) => {
    dictId.value = data.id;
    setProps({ searchInfo: { dictId: unref(dictId) } });
    reload();
  });

  // 自适应列配置
  const adaptiveColProps: Partial<ColEx> = {
    xs: 24, // <576px
    sm: 24, // ≥576px
    md: 24, // ≥768px
    lg: 12, // ≥992px
    xl: 12, // ≥1200px
    xxl: 8, // ≥1600px
  };

  const [registerTable, { reload, setProps }] = useTable({
    rowKey: 'dictId',
    api: itemList,
    columns: dictItemColumns,
    formConfig: {
      baseColProps: adaptiveColProps,
      labelAlign: 'right',
      labelCol: {
        offset: 1,
        xs: 24,
        sm: 24,
        md: 24,
        lg: 9,
        xl: 7,
        xxl: 4,
      },
      wrapperCol: {},
      schemas: dictItemSearchFormSchema,
      autoSubmitOnEnter: true,
      actionColOptions: {
        span: 8,
      },
    },
    striped: true,
    useSearchForm: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    immediate: false,
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      fixed: undefined,
    },
  });

  /**
   * 新增
   */
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 编辑
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /**
   * 删除
   */
  async function handleDelete(record) {
    await deleteItem({ id: record.id }, reload);
  }

  /**
   * 打开复制弹窗
   */
  function handleOpenCopyModal(record) {
    currentCopyRecord.value = record;
    openCopyModal(true);
  }

  /**
   * 处理复制确认
   */
  async function handleCopyConfirm() {
    if (!copyInputValue.value.trim()) {
      // 可以添加输入验证提示
      return;
    }

    if (!currentCopyRecord.value) return;

    try {
      copyLoading.value = true;
      // 调用复制接口，传入原记录ID和新名称
      await copyItem({
        id: currentCopyRecord.value.id,
        itemText: copyInputValue.value.trim(),
      });
      // 复制成功后刷新表格
      reload();
    } finally {
      closeModal();
      copyLoading.value = false;
    }
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
      {
        label: '复制',
        onClick: handleOpenCopyModal.bind(null, record),
      },
    ];
  }

  function getRowClassName(record) {
    return record.status == 0 ? prefixCls : '';
  }
</script>

<style scoped lang="less">
  @prefix-cls: ~'@{namespace}-row-invalid';

  :deep(.@{prefix-cls}) {
    background: #f4f4f4;
    color: #bababa;
  }
</style>
